<template>
  <div class="login flex justify-center items-center h-full">
  <div class="content">
    <h1 class="text-center mb-[18px] text-[#707070]">后台管理系统</h1>
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules">
      <el-form-item prop="username">
        <el-input v-model="ruleForm.username" size="medium">
          <i slot="prefix" class="el-input__icon el-icon-user"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="ruleForm.password" type="password" size="medium">
          <i slot="prefix" class="el-input__icon el-icon-lock"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input v-model="ruleForm.code" size="medium" class="w-[60%]">
          <i slot="prefix" class="el-input__icon el-icon-key"></i>
        </el-input>
          <div class="w-[30%] float-right mx-[5%] cursor-pointer" @click="updateCode">
            <img :src="srcImg" alt="" >
          </div>
      </el-form-item>
      <el-form-item>
      <el-button type="primary" class="w-full" @click="submitForm('ruleForm')">登录</el-button>
  </el-form-item>
    </el-form>
  </div>
  </div>
</template>
<script>
import { authCode, authLogin } from '@/api/login'
import { encrypt } from '@/utils/rsaEncrypt'
import Cookies from 'js-cookie'
export default {
  name: '',
  components: {},
  data () {
    return {
      ruleForm: {
        username: '', // 用户名
        password: '', // 密码
        code: '', // 验证码
        uuid: '' // 验证码id
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ]
      },
      srcImg: '' // 图片
    }
  },
  // 计算属性
  computed: {},
  watch: {},
  mounted () {
    this.updateCode()
  },
  methods: {
    // 更新验证码
    async updateCode () {
      const { img, uuid } = await authCode()
      this.srcImg = img
      this.ruleForm.uuid = uuid
      // await authCode().then(res => {
      // console.log(res)
      // const { img } = res
      // this.srcImg = img
      // })
    },
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // console.log('submit!')
          this.ruleForm.password = encrypt(this.ruleForm.password)
          authLogin(this.ruleForm).then(res => {
            console.log(res)
            const { token, user } = res
            Cookies.set('token', token)
            localStorage.setItem('user', JSON.stringify(user))
            this.$router.push('/')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.login {
  background: url('~@/assets/images/bg.jpg') no-repeat;
  background-size: cover;
}
.content{
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  background-color: #fff;
  border-radius: 6px;
  width: 385px;
  padding: 25px 25px 5px 25px;
}
</style>
